package com.dux.commons.configuration;


import java.io.File;
import java.net.InetSocketAddress;
import java.util.regex.Pattern;

import com.dux.commons.configuration.PropertyTransformer;
import com.dux.commons.configuration.transformers.BooleanTransformer;
import com.dux.commons.configuration.transformers.ByteTransformer;
import com.dux.commons.configuration.transformers.CharTransformer;
import com.dux.commons.configuration.transformers.ClassTransformer;
import com.dux.commons.configuration.transformers.DoubleTransformer;
import com.dux.commons.configuration.transformers.EnumTransformer;
import com.dux.commons.configuration.transformers.FileTransformer;
import com.dux.commons.configuration.transformers.FloatTransformer;
import com.dux.commons.configuration.transformers.InetSocketAddressTransformer;
import com.dux.commons.configuration.transformers.IntegerTransformer;
import com.dux.commons.configuration.transformers.LongTransformer;
import com.dux.commons.configuration.transformers.PatternTransformer;
import com.dux.commons.configuration.transformers.ShortTransformer;
import com.dux.commons.configuration.transformers.StringTransformer;
import com.dux.commons.utils.ClassUtils;

/**
 * 这个类负责创建转换器实例。<br>
 * 这个类会通过调用共享的实例，来返回转换器来节省消耗。
 * 
 * @author 杜祥
 * @create 2013-6-29
 */
public class PropertyTransformerFactory
{
	/**
	 * 返回对应的属性转换器，如果不能创建新的则抛出{@link com.game.commons.configuration.TransformationException}
	 * 
	 * @param clazzToTransform
	 *           转换器名称
	 * @param tc
	 *            {@link com.game.commons.configuration.PropertyTransformer} 的实现类，将被实例化返回。
	 * @return 返回实例的属性转换器
	 * @throws TransformationException
	 *             不能实例化指定的转换器类型，抛出此异常。
	 */
	@SuppressWarnings("rawtypes")
	public static PropertyTransformer newTransformer(Class clazzToTransform, Class<? extends PropertyTransformer> tc)
		throws TransformationException
	{

		if(tc == PropertyTransformer.class)
		{
			tc = null;
		}

		if(tc != null)
		{
			try
			{
				return tc.newInstance();
			}
			catch(Exception e)
			{
				throw new TransformationException("不能实例化转换器。", e);
			}
		}
		else
		{
			if(clazzToTransform == Boolean.class || clazzToTransform == Boolean.TYPE)
			{
				return BooleanTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == Byte.class || clazzToTransform == Byte.TYPE)
			{
				return ByteTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == Character.class || clazzToTransform == Character.TYPE)
			{
				return CharTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == Double.class || clazzToTransform == Double.TYPE)
			{
				return DoubleTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == Float.class || clazzToTransform == Float.TYPE)
			{
				return FloatTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == Integer.class || clazzToTransform == Integer.TYPE)
			{
				return IntegerTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == Long.class || clazzToTransform == Long.TYPE)
			{
				return LongTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == Short.class || clazzToTransform == Short.TYPE)
			{
				return ShortTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == String.class)
			{
				return StringTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform.isEnum())
			{
				return EnumTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == File.class)
			{
				return FileTransformer.SHARED_INSTANCE;
			}
			else if(ClassUtils.isSubclass(clazzToTransform, InetSocketAddress.class))
			{
				return InetSocketAddressTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == Pattern.class)
			{
				return PatternTransformer.SHARED_INSTANCE;
			}
			else if(clazzToTransform == Class.class)
			{
				return ClassTransformer.SHARED_INSTANCE;
			}
			else
			{
				throw new TransformationException("转换器 [" + clazzToTransform.getName() + "]不存在！");
			}
		}
	}
}
